//
//  SCHDExamnitionViewController.m
//  Arts1
//
//  Created by 孤星晨宇 on 15/9/29.
//  Copyright © 2015年 Arts1. All rights reserved.
//

#import "SCHDExamnitionViewController.h"
#import <AFNetworking.h>
#import "CollegeMainContentView.h"
#import "ShareViewController.h"
#import "AllHeader.h"
#import "SpaceContentModel.h"

// 高校  试卷详情、历年考题   数据接口
//#define  sdURL @"http://arts1.dlab.com.cn/app/paperinfo"
#define  sdURL @"http://arts1.cn/app/paperinfo"

@interface SCHDExamnitionViewController ()<UIWebViewDelegate>

// 自定义navigationbar
@property (nonatomic, strong) CollegeMainContentView *navigationview;

//  展示内容
@property (nonatomic, strong) NSString *textString;

//
@property (nonatomic, strong) UIScrollView *scrollview;

// 展示视图
@property (nonatomic, strong) UIWebView *webView;

// 分享视图
@property (nonatomic, strong) ShareViewController *shareVC;

//
@property (nonatomic, strong) SpaceContentModel *content;

@end

@implementation SCHDExamnitionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBackgroundColor;
    [self requestData];
    // Do any additional setup after loading the view.
    [self createcontactView];
    [self createnavigationView];
    [self createShareView];
}


//请求数据
- (void)requestData
{
    
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *pardic = @{@"id":_ID};
    
    [manage GET:sdURL parameters:pardic
        success:^(AFHTTPRequestOperation *operation, id responseObject) {
            // 请求成功的时候调用这个block
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
            // 请求返回的状态，当status = 1 时有数据
            NSInteger status = [dic[@"status"] integerValue];
            if (status == 1) {
                NSDictionary *dict = dic[@"data"];
                _content = [[SpaceContentModel alloc] init];
                [_content setValuesForKeysWithDictionary:dict];
                _textString = [NSString htmlEntityDecode:_content.content];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                _navigationview.content = _content;
                [_webView loadHTMLString:_textString baseURL:nil];
            });
            NSLog(@"高校历年考题 ===数据请求--%@", dic);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            // 请求失败的时候调用调用这个block
            NSLog(@"请求失败");
        }];
    
}

/**
 *  自定义navigationview
 */
- (void)createnavigationView{
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [UIView viewWideForShadowColor:whiteView];
    [self.view addSubview:whiteView];
    
    // 自定义UINavigationBar
    _navigationview = [[CollegeMainContentView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
    [whiteView addSubview:_navigationview];
    // 返回按钮点击事件
    [_navigationview.backButton addTarget:self action:@selector(backBefore) forControlEvents:(UIControlEventTouchUpInside)];
    
    // 分享按钮点击事件
    [_navigationview.shareBTN addTarget:self action:@selector(shareMessage:) forControlEvents:(UIControlEventTouchUpInside)];
    
}


//  创建联系我们页面
- (void)createcontactView{
    
    _scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT - 44)];
    _scrollview.backgroundColor = kBackgroundColor;
    _scrollview.contentSize = CGSizeMake(0, self.view.height);
    _scrollview.alwaysBounceVertical = YES;
    
    UIView *topShadowView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, 12.5)];
    topShadowView.backgroundColor = [UIColor whiteColor];
    topShadowView.layer.cornerRadius = 5;
    //    [UIView viewWideForShadowColor:topShadowView];
    [_scrollview addSubview:topShadowView];
    
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(10,15, SCREEN_WIDTH - 20, SCREEN_HEIGHT - 15)];
    _webView.userInteractionEnabled = YES;
    _webView.delegate = self;
    _webView.backgroundColor = [UIColor whiteColor];
    [UIView viewShadowColor:_webView];
    [_scrollview addSubview:_webView];
    
    
    UIImageView *mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, 10)];
    mainImageView.backgroundColor = [UIColor whiteColor];
    mainImageView.image = [UIImage imageNamed:@"colorImage"];
    [UIView viewRediusForLeftAndReightOfTop:mainImageView withRedius:5];
    [_scrollview addSubview:mainImageView];
    
    
    [self.view addSubview:_scrollview];
}

#pragma mark ---- UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    /**
     *  通过JS命令获取加载完成的webView的高度
     @"document.body.scrollHeight"
     */
    CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
    /**
     *  重新设置webView的高度
     */
    _webView.frame = CGRectMake(10, 20 , SCREEN_WIDTH - 20, height + 30 + 500);
    _scrollview.contentSize = CGSizeMake(0, 25 + height + 30);
    
}

// 创建分享选项卡
- (void)createShareView{
    _shareVC = [[ShareViewController alloc] init];
    [self.view addSubview:_shareVC.view];
    _shareVC.view.frame = CGRectMake(0,SCREEN_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT);
}

// 分享
- (void)shareMessage:(UIButton *)btn{
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
        _shareVC.view.frame = CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
    } completion:^(BOOL finished) {
        
    }];
}




// 返回上一层
- (void)backBefore{
    [self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
